package com.dylon.seapiweb.controller;

import com.dylon.common.base.AjaxResult;
import com.dylon.common.page.TableDataInfo;
import com.dylon.common.utils.ShiroUtils;
import com.dylon.common.utils.StringUtils;
import com.dylon.system.domain.SysUsers;
import com.dylon.system.service.ISysRoleService;
import com.dylon.system.service.ISysUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController extends BaseController {

    private String prefix = "system/user";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;


//    @Autowired
//    private SysPasswordService passwordService;

    @RequiresPermissions("system:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    @RequiresPermissions("system:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUsers user){
        startPage();
        List<SysUsers> list = userService.selectUserList(user);
        return getDataTable(list);
    }

//    @RequiresPermissions("system:user:export")
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(SysUsers user) {
//        List<SysUsers> list = userService.selectUserList(user);
//        ExcelUtil<SysUsers> util = new ExcelUtil<SysUsers>(SysUsers.class);
//        return util.exportExcel(list, "用户数据");
//    }

//    @RequiresPermissions("system:user:import")
//    @PostMapping("/importData")
//    @ResponseBody
//    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
//        ExcelUtil<SysUsers> util = new ExcelUtil<SysUsers>(SysUsers.class);
//        List<SysUsers> userList = util.importExcel(file.getInputStream());
//        String operName = getSysUser().getLoginName();
//        String message = userService.importUser(userList, updateSupport, operName);
//        return AjaxResult.success(message);
//    }

//    @RequiresPermissions("system:user:view")
//    @GetMapping("/importTemplate")
//    @ResponseBody
//    public AjaxResult importTemplate(){
//        ExcelUtil<SysUsers> util = new ExcelUtil<SysUsers>(SysUsers.class);
//        return util.importTemplateExcel("用户数据");
//    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
        mmap.put("roles", roleService.selectRoleAll());
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("system:user:add")
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(SysUsers user) {
        if (StringUtils.isNotNull(user.getUserId()) && SysUsers.isAdmin(user.getUserId())) {
            return error("不允许修改超级管理员用户");
        }
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassWord(DigestUtils.md5Hex(user.getPassWord()));
        user.setCreateBy(getSysUser().getUserName());
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap){
        mmap.put("user", userService.selectUserById(userId));
        mmap.put("roles", roleService.selectRolesByUserId(userId));
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("system:user:edit")
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(SysUsers user) {
        if (StringUtils.isNotNull(user.getUserId()) && SysUsers.isAdmin(user.getUserId())){
            return error("不允许修改超级管理员用户");
        }
        user.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(userService.updateUser(user));
    }

    @RequiresPermissions("system:user:resetPwd")
    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Long userId, ModelMap mmap){
        mmap.put("user", userService.selectUserById(userId));
        return prefix + "/resetPwd";
    }

    @RequiresPermissions("system:user:resetPwd")
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwdSave(SysUsers user){
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassWord(DigestUtils.md5Hex(user.getPassWord()));
        return toAjax(userService.resetUserPwd(user));
    }

    @RequiresPermissions("system:user:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        try{
            return toAjax(userService.deleteUserByIds(ids));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(SysUsers user)
    {
        return userService.checkAccountUnique(user.getAccount());
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(SysUsers user)
    {
        return userService.checkPhoneUnique(user);
    }

    /**
     * 校验email邮箱
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(SysUsers user)
    {
        return userService.checkEmailUnique(user);
    }

    /**
     * 用户状态修改
     */
    @RequiresPermissions("system:user:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysUsers user)
    {
        return toAjax(userService.changeStatus(user));
    }
}